import Vue from 'vue'
import Vuex from 'vuex'

import {
  sysNewsContentsQuery,
  sysNewsContentsOpenApiQuery,
  sysNewsContentsDelete,
  sysNewsContentsAdd,
  sysNewsContentsUpdate,
  sysNewsContentsDeleteBatch,
  sysNewsContentsQueryById,
  sysNewsContentsQueryId,
  sysNewsContentsOpenApiQueryPost,
  sysNewsContentsBatchChangeAuditStatus,
  sysNewsContentsBatchChangeStatus,
  sysNewsContentsDragSort,
} from '@/api/admin/collect/sysNewsContents'

Vue.use(Vuex)

const state = {
  homeArticle: {},
}

const getters = {
  homeArticle(state) {
    return state.homeArticle
  },
}

const actions = {
  //文章内容增删改查
  async queryNewsContents({ commit }, listQuery) {
    let res = await sysNewsContentsQuery({ ...listQuery })
    return res.data.code == 0 ? res.data : Promise.reject(new Error('faile'))
  },
  async deleteNewsContents({ commit }, row) {
    let res = await sysNewsContentsDelete(row.id)
    return res.data.code == 0 ? res.data : Promise.reject(new Error('faile'))
  },
  async addNewsContents({ commit }, data) {
    let res = await sysNewsContentsAdd(data)
    return res.data.code == 0 ? res.data : Promise.reject(new Error('faile'))
  },
  async updateNewsContents({ commit }, data) {
    let res = await sysNewsContentsUpdate(data)
    return res.data.code == 0 ? res.data : Promise.reject(new Error('faile'))
  },
  async deleteBatchNewsContents({ commit }, data) {
    let res = await sysNewsContentsDeleteBatch(data)
    return res.data.code == 0 ? res.data : Promise.reject(new Error('faile'))
  },
  async queryOpenApiNewsContents({ commit }, listQuery) {
    let res = await sysNewsContentsOpenApiQuery({ ...listQuery })
    return res.data.code == 0 ? res.data : Promise.reject(new Error('faile'))
  },
  async sysNewsContentsOpenApiQueryPost({ commit }, listQuery) {
    let res = await sysNewsContentsOpenApiQueryPost({ ...listQuery })
    return res.data.code == 0 ? res.data : Promise.reject(new Error('faile'))
  },
  //通过id出现文章
  async queryNewsContentsById({ commit }, id) {
    let res = await sysNewsContentsQueryById(id)
    return res.data.code == 0 ? res.data : Promise.reject(new Error('faile'))
  },
  //通过id出现文章
  async queryNewsContentsByIds({ commit }, id) {
    let res = await sysNewsContentsQueryId(id)
    return res.data.code == 0 ? res.data : Promise.reject(new Error('faile'))
  },
  //批量修文章刊审核状态
  async changeAuditStatusBatchNewsContents({ commit }, data) {
    let res = await sysNewsContentsBatchChangeAuditStatus(data)
    return res.data.code == 0 ? res.data : Promise.reject(new Error('faile'))
  },
  //批量修改文章发布状态
  async changeStatusBatchNewsContents({ commit }, data) {
    let res = await sysNewsContentsBatchChangeStatus(data)
    return res.data.code == 0 ? res.data : Promise.reject(new Error('faile'))
  },
  //拖动排序
  async dragSortNewsContents({ commit }, data) {
    let res = await sysNewsContentsDragSort(data)
    return res.data.code == 0 ? res.data : Promise.reject(new Error('faile'))
  },
}

const mutations = {
  setHomeArticle: (state, d) => {
    if (!d) return
    state.homeArticle = d
  },
}

export default {
  state,
  getters,
  actions,
  mutations,
}
